<?php
//网站新闻数据采集
namespace app\wumei\controller;


use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use app\wumei\service\CollectionService;
class CollectionController extends AdminBaseController
{


    //列表展示
    public function index()
    {


       $where = [];
       $where[]=['status','=',1];
       $list = Db::name('collection')->order('id desc')->paginate(10);
       $page = $list->render();//分页

       $this->assign('page',$page);
       $this->assign('list',$list);

        return $this->fetch();
    }

    //添加页面
    public function add()
    {
        return $this->fetch();
    }


    //修改页面
    public function edit()
    {
        //获取id,查询数据

        $id = request()->param('id');

        $data = Db::name('Collection')->find($id);
        $data['filter_rule'] =explode(',',$data['filter_rule']);

        $this->assign('data',$data);
        return $this->fetch();
    }

    //执行添加或者修改
    public function post()
    {
        $post = request()->param();
        $post['filter_rule'] = implode(',',$post['filter_rule']);
            foreach($post['code'] as $k=>$v){
                $post[$k] = htmlspecialchars_decode($v);
            }

            unset($post['code']);


        if(isset($post['id'])){
            $res = Db::name('Collection')->where(['id'=>$post['id']])->update($post);


        }else{

            $post['datetime'] = time();
            $res = Db::name('Collection')->insertGetId($post);
        }

        if($res){
           return  $this->success('操作成功','/wumei/Collection/index');
        }else{
            return  $this->error('操作失败');
        }

    }

    //预览采集
    public function getTest()
    {
        $id = request()->param('id');

        $info = Db::name('Collection')->find($id);
        if($info){
            if($info['match_url']){
                $url = str_replace('{*}',1,$info['match_url']);
                $CollectionService = new CollectionService();
                //采集一个页面
                $lst = $CollectionService->DownOnePage($url,$info);

                $content = $CollectionService->getArt($info['pre_url'],$info);

                $this->assign('url',$url);
                $this->assign('content',$content);
                $this->assign('lst',$lst);
                $this->assign('info',$info);

//                dump($url);
//                dump($content);
//                dump($lst);
//                dump($info);
                return $this->fetch();
            }else{
                return $this->error('没有配置采集地址');
            }
        }else{
            return $this->error('数据参数丢失，请重新编辑');
        }


    }

    //开始采集
    public  function getStarts()
    {
        //当前页码
        $pages = request()->param('pages');
        if($pages<1){
            $pages=1;
        }
        $id = request()->param('id');


            $info = Db::name('Collection')->find($id);
            Db::name('Collection')->where(['id'=>$id])->setField('datetime',time());




        if($info){
            if($pages > $info['end_pages']){
                //转到获取内容
                $this->success('页面采集完毕现转到采集内容','/wumei/Collection/getStorage/id/'.$id.'.html');
            }else{
                $next_page = $pages +$info['add_pages'];
                $url = str_replace('{*}',$pages,$info['match_url']);


                $Collection = new CollectionService();
//                dump($info);
                $lst = $Collection->DownOnePage($url,$info);

                if(!empty($lst)){
                
                    foreach($lst as $key=>$val){
                        $u =  Db::name('collection_url')->where(['url'=>$val['link']])->find();
                        if($u){
                            continue;
                        }


                        if(!empty($val['title'])){

                            $data = [];
                            $data['c_idx'] = $id;
                            $data['c_title'] = str_replace('&nbsp;','',trim($val['title']));
                            $data['url'] = $val['link'];
                            $data['is_storage'] =0;
                            $data['datetime'] = time();
                            $data['source'] = $info['source'];
                            Db::name('collection_url')->insert($data);
                        }
                    }
                }
                if($pages == $info['end_pages']){
                    return $this->success('','/wumei/Collection/getStarts/id/'.$id.'/pages/'.$next_page.'.html',0);
                }else{
                    return $this->success('继续采集第 '.$next_page.' 页的内容','/wumei/Collection/getStarts/id/'.$id.'/pages/'.$next_page.'.html');
                }

            }
        }


        echo  '开始采集';
    }

    //入库
    public function getStorage()
    {
        $id = request()->param('id');
        $lst = Db::name('Collection_url')->where(['c_idx'=>$id,'is_storage'=>0])->order('id desc')->select()->toArray();


        if($lst){
            session('caiji_url_'.$id,$lst);
            return $this->success('进行下一步入库操作','/wumei/Collection/addStorage/id/'.$id.'/num/0.html','',1);
        }else{
            return $this->success('没有内容了，采集结束','/wumei/Collection/index.html');
        }
    }

    //入库操作
    public function addStorage()
    {
        $id = request()->param('id');
        $num = request()->param('num');
        $info = session('caiji_'.$id);
        if(!$info){
            $info = Db::name('Collection')->find($id);
                session('caiji_'.$id,$info);
        }

        $caiji_url = session('caiji_url_'.$id);
        if($caiji_url){
            if(count($caiji_url)>$num){
                $CollectionService = new CollectionService();


                $content = $CollectionService->getArt($caiji_url[$num]['url'],$info);
                if(!empty($content)){
                    $data = [];
                    if(!empty($content['img'])){
                        $data['thumb'] = $content['img'][0];
                    }
                    $data['source'] = $info['source'];
                    $data['datetime'] = time();
                    $data['title'] = str_replace('&nbsp;',' ',trim($content['title']));
                    $data['details'] = $content['body'];
                    if(!empty($data['details'])){
                        $data['is_storage'] = 1;
                    }else{
                        $data['is_storage'] = 2;
                    }

                    $result = Db::name('Collection_url')->where(['id'=>$caiji_url[$num]['id']])->data($data)->update();
                }

                //这里每次修改都需重新跳转进来，应该是防止封IP，增加请求时间，要是用foreach循环，就算也模拟手动操作，每次执行一次sleep一下，但是php有连接时间，容易超时，所以还是这样最好（刘登辉分析（代码不是我写的，我是抄的））
                return $this->success('第 '.($num+1).' 条记录入库操作','/wumei/Collection/addStorage/id/'.$id.'/num/'.($num+1),'',1);

            }else{
                return $this->success('没有内容了','/wumei/Collection/index.html');
            }
        }else{
            return $this->success('没有内容了','/wumei/Collection/index.html');
        }

    }

    //获取采集的新闻的列表
    public function getCaijiNewsList()
    {
       $c_idx = request()->param('id');
       $where = ['c_idx'=>$c_idx,'is_storage'=>1];
       $lst =  Db::name('Collection_url')->where($where)->order('id desc')->paginate(15);

       $page = $lst->render();
       $this->assign("page",$page);
       $this->assign('c_idx',$c_idx);
       $this->assign('list',$lst);
        return $this->fetch();
    }



    //修改采集的新闻
    public function getCaijiNewsEdit()
    {
        $param = request()->param();
        if(request()->isAjax()){

            $param['details']=htmlspecialchars_decode($param['details']);
            $data['datetime'] = time();
            $result = Db::name('Collection_url')->where(['id'=>$param['id']])->save($param);
            if($result){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }else{
            if(isset($param['id'])){
                $id = $param['id'];
                $info = Db::name('Collection_url')->find($id);
                if($info){
                    $this->assign('id',$id);
                    $this->assign('info',$info);
                    return $this->fetch();
                }else{
                    return $this->error('未查询到要修改的数据，请重新编辑');
                }

            }else{
                return $this->error('数据参数丢失，请重新编辑');

            }


        }
    }
    //数据采集的新闻删除
    public function getCaijiNewsDel()
    {
        if(request()->isAjax()){
            $id = request()->param('id');
            if(!empty($id)){
                $res = Db::name('Collection_url')->delete($id);
                if($res){
                    return $this->success('删除成功');
                }else{
                    return $this->error('删除失败');
                }

            }else{
                return $this->error('数据丢失，请重新请求');
            }
        }else{
            return $this->error('非法请求');
        }
    }

}